// 文章相关接口
import type {
  CreateArticleParams,
  ResArticleById,
  ResArticleList,
  ResArticleListParams,
  ResChannels,
  ResCreateArticle
} from '@/types/article'
import http from '@/utils/http'

enum API {
  // 获取频道列表
  CHANNEL_URL = '/channels',
  // 提交文章表单
  CREATEARTICLE_URL = '/mp/articles?draft=false',
  // 获取文章列表
  ARTICLELIST_URL = '/mp/articles',
  // 通过 id 删除文章
  DELARTICLE_URL = '/mp/articles/',
  // 获取文章详情
  GETARTICLE_URL = '/mp/articles/'
}

/**
 * 获取频道列表
 * @returns
 */
export const reqChannel = () => {
  return http.get<any, ResChannels>(API.CHANNEL_URL)
}

/**
 * 提交文章表单
 * @param {CreateArticleParams} data Body 参数
 * @returns
 */
export const reqCreateArticle = (data: CreateArticleParams) => {
  return http.post<any, ResCreateArticle>(API.CREATEARTICLE_URL, data)
}

/**
 * 获取文章列表
 * @returns
 */
export const reqArticleList = (params?: ResArticleListParams) => {
  return http.get<any, ResArticleList>(API.ARTICLELIST_URL, { params })
}

/**
 * 通过 id 删除文章
 * @param id 文章 id
 * @returns
 */
export const reqDelArticle = (id: string) => {
  return http.delete(API.DELARTICLE_URL + id)
}

/**
 * 通过 id 获取文章详情
 * @param id id
 * @returns
 */
export const reqGetArticleByID = (id: string) => {
  return http.get<any, ResArticleById>(API.GETARTICLE_URL + id)
}

/**
 * 更新文章
 * @param {CreateArticleParams} data Body 参数
 * @returns
 */
export const reqUpdateArticle = (data: CreateArticleParams) => {
  return http.put<any, ResCreateArticle>(`/mp/articles/${data.id}?draft=false`, data)
}
